package com.faxsun.core.persist.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import mtons.modules.persist.impl.DaoImpl;

import com.faxsun.core.persist.dao.LiveInteractionDao;
import com.faxsun.core.persist.entity.CommentPO;
import com.faxsun.core.persist.entity.LiveInteractionPO;

public class LiveInteractionDaoImpl extends DaoImpl<LiveInteractionPO>
		implements LiveInteractionDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5410987473787863284L;

	protected LiveInteractionDaoImpl() {
		super(LiveInteractionPO.class);
	}

	@Override
	public Number getLiveNop(long liveId) {
		Criteria c = super.createCriteria(LiveInteractionPO.class);
		c.add(Restrictions.eq("liveId", liveId));
		c.setProjection(Projections.rowCount());
		return Long.parseLong(c.uniqueResult().toString());
	}

	@Override
	public Number getLiveNoc(long liveId, int type) {
		final int liveComment = 3;
		Criteria c = super.createCriteria(CommentPO.class);
		c.add(Restrictions.eq("toId", liveId));
		c.add(Restrictions.eq("type", liveComment));
		c.setProjection(Projections.rowCount());
		return Long.parseLong(c.uniqueResult().toString());
	}

	@Override
	public LiveInteractionPO find(long liveId, long userId) {

		return super.findUnique(
				"from LiveInteractionPO where userId=? and liveId=?", userId,
				liveId);
	}

    @Override
    public Number getAllLiveViewer() {
        Criteria c = super.createCriteria(LiveInteractionPO.class);
        c.setProjection(Projections.rowCount());
        return Long.parseLong(c.uniqueResult().toString());
    }
}
